package com.leff.cards.data;

import java.util.ArrayList;

import com.leff.cards.util.Secrets;
import com.leff.cards.util.FacebookSession;
import com.leff.cards.web.QueryDispatcher;
import com.leff.cards.web.QueryDispatcher.OnQueryCompleteListener;
import com.leff.cards.web.requests.GetGroupsRequest;
import com.leff.cards.web.requests.GetInvitesRequest;
import com.leff.cards.web.requests.LoginRequest;
import com.leff.notifications.NotificationCenter;
import com.leff.notifications.NotificationListener;

public class AppState implements Secrets, NotificationListener {
	
	private User mUser;
	private ArrayList<Group> mGroups;
	private ArrayList<Invitation> mInvites;
	private Group mActiveGroup;
	
	public static final String APP_STATE_INITIALIZED = "AppStateInitialized";
	
	public static final String GROUPS_REFRESHED = "GroupsRefreshed";
	public static final String INVITES_REFRESHED = "InvitesRefreshed";
	
	public AppState() {
		mGroups = new ArrayList<Group>();
		mInvites = new ArrayList<Invitation>();

		NotificationCenter.registerListener(this, FacebookSession.FB_LOGIN_COMPLETE);
		NotificationCenter.registerListener(this, LoginRequest.LOGGED_IN);
		NotificationCenter.registerListener(this, GetGroupsRequest.GROUPS_RECEIVED);
		NotificationCenter.registerListener(this, GetInvitesRequest.INVITES_RECEIVED);
	}
	
	public User getUser() {
		return mUser;
	}
	public ArrayList<Group> getGroups() {
		return mGroups;
	}
	public boolean setActiveGroup(long id) {
		for(Group G : mGroups) {
			if(G.id == id) {
				mActiveGroup = G;
				return true;
			}
		}
		return false;
	}
	public Group getActiveGroup() {
		return mActiveGroup;
	}
	
	public void refreshGroups() {
		GetGroupsRequest ggr = new GetGroupsRequest(mUser.id);
		new QueryDispatcher(ggr).execute();
	}
	
	public ArrayList<Invitation> getInvites() {
		return mInvites;
	}
	
	public void refreshAll() {
		refreshAll(false);
	}
	public void refreshAll(final boolean notify) {
		
		GetGroupsRequest ggr = new GetGroupsRequest(mUser.id);
		GetInvitesRequest gir = new GetInvitesRequest(mUser.fbid);
		
		new QueryDispatcher(new OnQueryCompleteListener() {
			public void onQueryComplete() {
				if(notify) {
					NotificationCenter.postNotification(APP_STATE_INITIALIZED, null);
				}
			}
		}, ggr, gir).execute();
	}
	
	public void verifyUser() {
		new QueryDispatcher(new LoginRequest(mUser.fbid, mUser.displayName)).execute();
	}
	
	public void beginLogin() {
		FacebookSession.getSession().login();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onNotificationPosted(String action, Object data, Object sender) {
		
		if(this.equals(sender)) {
			return;
		}
		
		if(action.equals(FacebookSession.FB_LOGIN_COMPLETE)) {
			mUser = (User)data;
			verifyUser();
		}
		else if(action.equals(LoginRequest.LOGGED_IN)) {
			mUser = (User)data;
			refreshAll(true);
		}
		else if(action.equals(GetGroupsRequest.GROUPS_RECEIVED)) {
			mGroups = (ArrayList<Group>)data;
			NotificationCenter.postNotification(GROUPS_REFRESHED, null);
		}
		else if(action.equals(GetInvitesRequest.INVITES_RECEIVED)) {
			mInvites = (ArrayList<Invitation>)data;
			NotificationCenter.postNotification(INVITES_REFRESHED, null);
		}
	}
	
	/*
	 * Singleton Management
	 */
	private static AppState instance;
	public static AppState get() {
		if(instance == null) {
			instance = new AppState();
		}
		return instance;
	}
}
